from flask import Blueprint, session, redirect, render_template, request
from utils import db
from utils import cache

# 登录相关功能
# 蓝图对象
od = Blueprint('order', __name__)


@od.route('/order/list')
def order_list():
    # return "订单列表"
    user_info = session.get('user_info')
    role = user_info['role']  # 1客户 2管理员
    if role == '2':
        print("管理员登录", role)
        # data_list = db.fetch_all("select * from `order`", [])
        data_list = db.fetch_all("select * from `order` left join userinfo on `order`.user_id = userinfo.id", [])
    else:
        print("客户登录", role)
        # data_list = db.fetch_all("select * from `order` where user_id=%s", [user_info['id'], ])
        data_list = db.fetch_all(
            "select * from `order` left join userinfo on `order`.user_id = userinfo.id where `order`.user_id=%s",
            [user_info['id'], ])

    status_dict = {
        1: {"text": "待执行", 'cls': "primary"},
        2: {"text": "正在执行", 'cls': "info"},
        3: {"text": "完成", 'cls': "success"},
        4: {"text": "失败", 'cls': "danger"},
    }

    print(data_list)
    # return render_template('stock.html')
    return render_template('order_list.html', data_list=data_list, status_dict=status_dict)
    # return "订单列表"


# "创建订单"
@od.route('/order/create', methods=['GET', 'POST'])
def order_create():
    if request.method == "GET":
        return render_template('order_create.html')

    url = request.form.get('url')
    count = request.form.get('count')

    # 写入数据库
    user_info = session.get('user_info')
    params = [url, count, user_info['id']]
    order_id = db.insert("insert into `order`(url, count,user_id,status) values (%s, %s,%s,1)", params)
    print(order_id)
    # 写入redis队列
    cache.push_queue(order_id)

    return redirect('/order/list')


@od.route('/order/delete')
def delete_list():
    return "删除订单"
